<?php


require "database.php";


require "../config/config.php";

$base=new Db($__conf__);

$username=$_POST["username"];
$password=$_POST["password"];


$___r___=$base->doSql('select * from tp_user where username="'.$username.'"');

$token=mt_rand(100000,999999);

if ($___r___) {
	if ( $___r___[0]["password"] != md5( $password ) ) {
		return json([
			"code"=>400,
			"msg"=>"System Error",
			"requestDate"=>date("Y-m-d H:i:s"),
			"data"=>null
		]);
	}else{
		
		$base->doSql('update tp_user set token="'.$token.'",logintime="'.time().'"  where username="'.$username.'"');
		return json([
			"code"=>200,
			"msg"=>"Successful",
			"requestDate"=>date("Y-m-d H:i:s"),
			"data"=>[
				"token"=>$token
			]
		]);
	}
}else{

	$base->doSql('insert into tp_user set username="'.$username.'",nickname="'.$username.'",password="'.md5($password).'",logintime="'.time().'",token="'.$token.'",addtime="'.time().'"');
	return json([
		"code"=>200,
		"msg"=>"Successful",
		"requestDate"=>date("Y-m-d H:i:s"),
		"data"=>[
			"token"=>$token
		]
	]);
}
